package top.infopub.mgr.fia.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.fia.FundsHandDto;
import top.infopub.enums.fia.FiaProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.dao.CommonDao;
import top.infopub.mgr.fia.dao.FundsHandDao;
import top.infopub.mgr.fia.domain.FundsHandDomain;
import top.infopub.mgr.fia.jsonvo.FundsHandJsonVo;
import top.infopub.mgr.fia.service.BankInfoService;
import top.infopub.mgr.fia.service.FiaProcessManageService;
import top.infopub.mgr.fia.service.FundsHandService;
import top.infopub.mgr.fia.vo.FundsHandVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.hr.dao.StaffInfoDao;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.service.SetupProService;
import top.infopub.model.common.HrOrgInfo;
import top.infopub.model.common.ProInfo;
import top.infopub.model.fia.BankInfo;
import top.infopub.model.fia.FiaProcessDetail;
import top.infopub.model.fia.FundsHand;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.util.Date;
import java.util.List;

@Service
public class FundsHandServiceImpl extends GenericServiceImpl<FundsHand, String> implements
		FundsHandService {
	@Autowired
	private FundsHandDao fundsHandDao;

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private StaffInfoDao staffInfoDao;

	@Autowired
	private ProjectAttachmentDao projectAttachmentDao;

	@Autowired
	private SetupProService setupProService;

	@Autowired
	private OAFlowService oaFlowService;

	@Autowired
	private FiaProcessManageService fiaProcessManageService;

	@Autowired
	private BankInfoService bankInfoService;
	
	@Override
	public GenericDao<FundsHand, String> getDao() {
		return fundsHandDao;
	}

    @Override
    public Page<FundsHandDomain> selectPageByDomainWhere(Page<FundsHandDomain> page,
                                                         FundsHandVo fundsHandVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<FundsHandDomain>)fundsHandDao.selectByDomainWhere(fundsHandVo);
    }

	@Override
	public FundsHandJsonVo queryDetailById(String id) {
		FundsHandJsonVo fundsHandJson = new FundsHandJsonVo();
		if (StringUtils.isNotBlank(id)) {
			//基础信息
			FundsHandDto fundsHandDto = fundsHandDao.queryDetailById(id);
			fundsHandJson.setInfo(fundsHandDto);

			//附件信息表
			List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(fundsHandDto.getId(), ProjectFileTypeEnum.FILE_TYPE60.getCode(), null);
			fundsHandJson.setFileAtt(setupProService.fileAttaListToArray(projectAttaList));
		} else {
			//基础信息
			FundsHandDto fundsHandDto = new FundsHandDto();
			AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
			StaffInfo staffInfo = staffInfoDao.selectByPrimaryKey(vo.getHrStaffId());
			if (staffInfo == null) {
				fundsHandDto.setStaffName(vo.getRealName());
			} else {
				fundsHandDto.setStaffName(staffInfo.getStaffName());
			}
			fundsHandJson.setInfo(fundsHandDto);

			//项目信息列表
			List<ProInfo> proInfoList = commonDao.queryCapSetupProList(ProProcesssStatusEnum.PRO_STATUS9.getCode());
			fundsHandJson.setProInfoList(proInfoList);
		}
		//银行信息列表
		List<BankInfo> bankInfoList = bankInfoService.queryBankInfoList("1");
		fundsHandJson.setBankInfoList(bankInfoList);
		//组织架构单位列表
		List<HrOrgInfo> orgInfoList = commonDao.queryHrOrgInfoList(null);
		fundsHandJson.setOrgInfoList(orgInfoList);
		return fundsHandJson;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public RspVo edit(FundsHandDto fundsHandDto, List<ProjectAttachment> attachmentList, Integer submitType, AuthorizationVO vo) {
		FundsHand tmpFh = new FundsHand();
		FundsHand fundsHand = new FundsHand();
		BeanUtils.copyProperties(fundsHandDto, fundsHand);
		if (StringUtils.isNotBlank(fundsHandDto.getId())) {
			//校验资金上交数据是否存在
			tmpFh = fundsHandDao.selectByPrimaryKey(fundsHandDto.getId());
			if (tmpFh == null) return RspVo.getFailureResponse(400, "融资付息数据不存在");
			//判断审批状态
			if (StringUtils.isNotBlank(tmpFh.getProcessStatus())
					&& (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), tmpFh.getProcessStatus())
					|| StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), tmpFh.getProcessStatus()))) {
				return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
			}
			fundsHand.setUptBy(vo.getId());
			fundsHand.setUptTime(new Date());
			fundsHandDao.updateByIdSelective(fundsHand);
		} else {
			fundsHand.setCodes(IdFactory.getUUIDUpper());
			fundsHand.setAddBy(vo.getId());
			fundsHand.setAddTime(new Date());
			fundsHand.setUptBy(vo.getId());
			fundsHand.setUptTime(new Date());
			fundsHandDao.insertSelective(fundsHand);
		}

		//更新附件信息
		projectAttachmentDao.deleteByWhere(fundsHand.getId(), ProjectFileTypeEnum.FILE_TYPE60.getCode(), null);
		if (attachmentList != null && attachmentList.size() > 0) {
			projectAttachmentDao.insertBatch(attachmentList);
		}

		if (submitType == 1) {
			if (org.apache.commons.lang.StringUtils.isNotBlank(tmpFh.getOaId())) {
				oaFlowService.finishOA(tmpFh.getOaId());
			}
			FundsHandJsonVo fundsHandJson = queryDetailById(fundsHand.getId());
			fundsHandJson.setProInfoList(null);
			fundsHandJson.setOrgInfoList(null);
			fundsHandJson.setBankInfoList(null);
			String detailJson = JSON.toJSONString(fundsHandJson);
			fiaProcessManageService.sendProcess(FiaProcessDetailTypeEnum.PRO_NODE_TYPE6, detailJson, fundsHand.getId(), fundsHand.getProCode(), vo);
		}
		return RspVo.getSuccessResponse();
	}

	@Override
	public RspVo modifyFinInfo(FundsHand fundsHand, AuthorizationVO vo, String processId) {
		//校验资金上交数据是否存在
		FundsHand tmpFh = fundsHandDao.selectByPrimaryKey(fundsHand.getId());
		if (tmpFh == null) return RspVo.getFailureResponse(400, "资金上交数据不存在");
		//判断审批状态
		if (!(StringUtils.isNotBlank(tmpFh.getProcessStatus())
				&& StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), tmpFh.getProcessStatus()))) {
			return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
		}

		fundsHand.setUptBy(vo.getId());
		fundsHand.setUptTime(new Date());
		//更新资金上交信息
		fundsHandDao.updateByIdSelective(fundsHand);

		FiaProcessDetail fpd = fiaProcessManageService.queryByProcessId(processId);
		FundsHandJsonVo fundsHandJson = JSON.parseObject(fpd.getDetailJson(), FundsHandJsonVo.class);
		fundsHandJson.getInfo().setActualAmt(fundsHand.getActualAmt());
		fundsHandJson.getInfo().setActualDate(fundsHand.getActualDate());
		fundsHandJson.getInfo().setBankName(fundsHand.getBankName());
		fundsHandJson.getInfo().setAccountNo(fundsHand.getAccountNo());
		fundsHandJson.getInfo().setFinMemo(fundsHand.getFinMemo());
		fiaProcessManageService.modifyDetailJsonById(fpd);
		return RspVo.getSuccessResponse();
	}
}